//
//  BaseViewController.m
//  KaneNote
//
//  Created by HuanJing on 2017/7/14.
//  Copyright © 2017年 HuanJing. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self networkDetect];
    [self initData];

}

//监测网络状态
- (void)networkDetect {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(networkChanged:)
                                                 name:kRealReachabilityChangedNotification
                                               object:nil];
    
}

- (void)networkChanged:(NSNotification *)notification {
    RealReachability *reachability = (RealReachability *)notification.object;
    ReachabilityStatus status = [reachability currentReachabilityStatus];
    if (self.networkChangedBlock) {
        self.networkChangedBlock(status);
    }
}

- (void)initData {
    //显示一个菊花
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication].windows lastObject] animated:YES];
    hud.label.text = @"请稍等, 程序正在初始化";
    //在多线程中请求数据
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        //如果方法实现了, 就调用
        if ([self respondsToSelector:@selector(fetchData)]) {
            [self fetchData];
        }
        //在主线程里面更新UI
        dispatch_async(dispatch_get_main_queue(), ^{
            [hud hideAnimated:YES];
            if (self.refreshDataBlock) {
                self.refreshDataBlock();
            }
        });
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:kRealReachabilityChangedNotification
                                                  object:nil];
}

@end
